var tags = liwe.module ( "tags" );

tags._cache = {};
tags.limit = 1000;

tags.init = function()
{

};

tags.list = function ( module_name, cback, reload )
{
	if ( ! reload && tags._cache [ module_name ] )
	{
		if ( cback ) cback ( tags._cache [ module_name ] );
	} else {
		tags.ajax ( { action: "tags.ajax.tags_list_all", module: module_name, limit: tags.limit }, 
			function ( v )
			{
				var res = {};
				var tgs = v [ 'tags' ], t, l = tgs.length;

				for ( t = 0; t < l; t ++ ) 
					res [ tgs [ t ] [ 'name' ] ] = tgs [ t ] [ 'id' ];

				tags._cache [ module_name ] = res;
				// console.debug ( "RES: %o", res );
				if ( cback ) cback ( tags._cache [ module_name ] );
			} );
	}
};

tags.convert = function ( tag_list )
{
	if ( ! tag_list ) return "";

	var t, l = tag_list.length;
	var s = new String.buffer ();

	for ( t = 0; t < l; t ++ )
		s.add ( tag_list [ t ] [ 'name' ] );

	return s.get ( "|" );
};
